var express=require('express');

var router=express.Router();

var mongoose=require('mongoose');
var Goods=require('../models/goods')
var User=require('../models/user');
//连接mongodb数据库
mongoose.connect('mongodb://127.0.0.1:27017/mall');

mongoose.connection.on("connected", function () {
  console.log("数据库连接成功")

});
mongoose.connection.on("error", function () {
  console.log("数据库连接失败")

});
mongoose.connection.on("disconnected", function () {
  console.log("数据库连接成功")

});
//获取用户列表

router.get("/list",function(req,res,next){
//获取前端传来数据
let page=parseInt(req.param("page"));
let pageSize=parseInt(req.param("pageSize"));
let sort=req.param("sort");
let skip=(page-1)*pageSize;

//分类 这里是存在all的 所以不能用parseInt来处理 ，出现了bug，已经解决
let priceLevel=req.param("priceLevel");
var priceGt = '',priceLte = '';
let params = {};

if (priceLevel!='all') {
    switch(priceLevel){
      case '0':priceGt=0;priceLte=100;break;
      case '1':priceGt=100;priceLte=500;break;
      case '2':priceGt=500;priceLte=1000;break;
      case '3':priceGt=1000;priceLte=1500;break;

    }
//参数导入
    params = {
        salePrice:{
            $gt:priceGt,
            $lte:priceLte
        }
      }
    }


let goodsModel=Goods.find(params).skip(skip).limit(pageSize);
goodsModel.sort({'salePrice':sort});
goodsModel.exec({},function(err,doc){
        if (err) {
            res.json({
                status:'1',
                msg:err.message
            });  
        }else{
            res.json({
                status:'0',
                msg:'',
                result:{
                    count: doc.length,
                    list:doc
                }
    
            })
        }
    })
});

//加入到购物车
router.post("/addCart", function (req,res,next) {
  var userId = '1',productId = req.body.productId;
  var User = require('../models/user');
  User.findOne({userId:userId}, function (err,userDoc) {
    if(err){
        res.json({
            status:"1",
            msg:err.message
        })
    }else{
        console.log("userDoc:"+userDoc);
        if(userDoc){
          var goodsItem = '';
          userDoc.cartList.forEach(function (item) {
              if(item.productId == productId){
                goodsItem = item;
                item.productNum ++;
              }
          });
          if(goodsItem){
            userDoc.save(function (err2,doc2) {
              if(err2){
                res.json({
                  status:"1",
                  msg:err2.message
                })
              }else{
                res.json({
                  status:'0',
                  msg:'',
                  result:'suc'
                })
              }
            })
          }else{
            Goods.findOne({productId:productId}, function (err1,doc) {
              if(err1){
                res.json({
                  status:"1",
                  msg:err1.message
                })
              }else{
                if(doc){
                  doc.productNum = 1;
                  doc.checked = 1;
                  userDoc.cartList.push(doc);
                  userDoc.save(function (err2,doc2) {
                    if(err2){
                      res.json({
                        status:"1",
                        msg:err2.message
                      })
                    }else{
                      res.json({
                        status:'0',
                        msg:'',
                        result:'suc'
                      })
                    }
                  })
                }
              }
            });
          }
        }
    }
  })
});
module.exports=router;